package com.yq.service.impl;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yq.core.entity.SysPowerEntity;
import com.yq.core.service.SysPowerService;
import com.yq.core.util.Page;
import com.yq.service.annotation.AzerothAutoService;
import com.yq.service.dao.BaseMapper;
import com.yq.service.dao.SysPowerMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 信  息: [yinquan] [2017-05-04] [1.0]
 * 描  述: Service 实现
 */
@AzerothAutoService("sysPowerService")
public class SysPowerServiceImpl extends BaseServiceImpl<SysPowerEntity, String> implements SysPowerService {

    private static Logger logger = LoggerFactory.getLogger(SysPowerServiceImpl.class);

    @Autowired
    private SysPowerMapper sysPowerMapper;

    public BaseMapper<SysPowerEntity, String> getBaseMapper() {
        return sysPowerMapper;
    }

    /** 查找顶部菜单 **/
    public List<Map<String, Object>> findMenuTopList(Map<String, Object> parameter) {
        return sysPowerMapper.findMenuTopList(parameter);
    }

    /** 查找左侧菜单 **/
    public List<Map<String, Object>> findMenuLeftList(Map<String, Object> parameter) {
        return sysPowerMapper.findMenuLeftList(parameter);
    }

    /** 菜单排序 **/
    public void updateOrder(Map<String, Object> parameter) {
        String uid = parameter.get("uid").toString();
        String nextuid = parameter.get("nextuid").toString();
        String updateBy = parameter.get("updateBy").toString();
        String orderFunType = parameter.get("orderFunType").toString();

        Date date = new Date();

        // 获取原对象
        SysPowerEntity sysPower = super.findUniqueById(uid);
        if(sysPower.getRelOrder()==null) {
            // 获取本级最大值
            int order = (int) sysPowerMapper.findMenuOrderMax(parameter);
            sysPower.setRelOrder(order + 1);
            sysPower.setUpdateBy(updateBy);
            sysPower.setUpdateAt(date);

            super.update(sysPower);
        } else {
            SysPowerEntity sysPowerNext = super.findUniqueById(nextuid);
            // 有排序值
            int temp = sysPower.getRelOrder();
            sysPower.setRelOrder(sysPowerNext.getRelOrder());
            sysPowerNext.setRelOrder(temp);

            sysPower.setUpdateBy(updateBy);
            sysPower.setUpdateAt(date);

            sysPowerNext.setUpdateBy(updateBy);
            sysPowerNext.setUpdateAt(date);

            super.update(sysPower);
            super.update(sysPowerNext);
        }

    }

    /** 菜单列表查询 **/
    public Page<Map<String, Object>> pageMenuList(Map<String, Object> parameter, Page page) {
        PageBounds pageBounds = new PageBounds(page.getPageCurrent(), page.getPageSize(), true);
        List<Map<String, Object>> list = sysPowerMapper.pageMenuList(parameter, pageBounds);
        PageList<Map<String, Object>> pageList = (PageList) list;
        page.setRows(pageList);
        page.setTotalPageNum(pageList.getPaginator().getTotalPages());// 总页数
        page.setTotal(pageList.getPaginator().getTotalCount());// 总记录数
        return page;
    }

    /** 菜单资源数据 **/
    public List<Map<String, Object>> pageMenuDataList() {
        return sysPowerMapper.pageMenuDataList();
    }

    /** 当前用户去权限数据 **/
    public List<Map<String, Object>> pagePowerDataList(Map<String, Object> parameter) {
        return sysPowerMapper.pagePowerDataList(parameter);
    }
}